package com.fr.web.platform;

import com.fr.base.FRContext;
import com.fr.base.core.ComparatorUtils;
import com.fr.data.TableData;
import com.fr.data.impl.JDBCDatabaseConnection;
import com.fr.privilege.authority.Authority;
import com.fr.report.script.core.FArray;
import com.fr.web.core.WebUtils;
import com.fr.web.platform.exception.RedirectException;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class PlatformUtils
{
  public static void redirect(HttpServletResponse paramHttpServletResponse, String paramString)
    throws Exception
  {
    paramHttpServletResponse.sendRedirect(paramString);
    throw new RedirectException();
  }

  public static Connection createConnection()
  {
    if (FRContext.getPlatformConnection() == null)
      return null;
    Connection localConnection = null;
    try
    {
      localConnection = FRContext.getPlatformConnection().createConnection();
    }
    catch (Exception localException)
    {
      FRContext.getLogger().log(Level.WARNING, localException.getMessage(), localException);
    }
    return localConnection;
  }

  public static Authority[] createSessionAuthorities(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
  {
    String[] arrayOfString;
    int j;
    HttpSession localHttpSession = paramHttpServletRequest.getSession(false);
    WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "fr_username");
    Object localObject = localHttpSession.getAttribute("fr_free_authority");
    Authority[] arrayOfAuthority = new Authority[0];
    if (localObject instanceof String)
    {
      arrayOfString = ((String)localObject).split(",");
      ArrayList localArrayList = new ArrayList();
      j = 0;
      int k = arrayOfString.length;
      while (j < k)
      {
        localArrayList.add(new Authority(arrayOfString[j]));
        ++j;
      }
      arrayOfAuthority = (Authority[])localArrayList.toArray(new Authority[localArrayList.size()]);
    }
    else if (localObject instanceof String[])
    {
      arrayOfString = (String[])localObject;
      int i = arrayOfString.length;
      arrayOfAuthority = new Authority[i];
      for (j = 0; j < i; ++j)
        arrayOfAuthority[j] = new Authority(arrayOfString[j]);
    }
    return arrayOfAuthority;
  }

  public static FArray obtainAuthority(Authority[] paramArrayOfAuthority)
  {
    int i;
    FArray localFArray = new FArray();
    if (paramArrayOfAuthority != null)
      for (i = 0; i < paramArrayOfAuthority.length; ++i)
        localFArray.add(paramArrayOfAuthority[i].getName());
    return localFArray;
  }

  public static int[] getColumnIndics(TableData paramTableData, String[] paramArrayOfString)
  {
    if (paramTableData == null)
      return new int[0];
    int[] arrayOfInt = new int[paramArrayOfString.length];
    for (int i = 0; i < arrayOfInt.length; ++i)
      arrayOfInt[i] = -1;
    try
    {
      for (i = 0; i < paramArrayOfString.length; ++i)
      {
        int j = 0;
        int k = paramTableData.getColumnCount();
        while (j < k)
        {
          if (ComparatorUtils.tableDataColumnNameEquals(paramArrayOfString[i], paramTableData.getColumnName(j)))
            arrayOfInt[i] = j;
          ++j;
        }
      }
    }
    catch (Exception localException)
    {
      FRContext.getLogger().log(Level.WARNING, localException.getMessage(), localException);
    }
    return arrayOfInt;
  }
}